package com.sanhai.android.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.sanhai.android.util.ImageUtil;
import com.sanhai.android.util.Util;


/**
 * Created by mahuihuang on 15/3/27.
 */
public class UserHeadImage extends ImageView {

	/*private int bgColos[] = new int[]{getResources().getColor(R.color.gold), getResources().getColor(R.color.greenyellow)
            , getResources().getColor(R.color.lightgreen), getResources().getColor(R.color.dodgerblue)
			, getResources().getColor(R.color.freshgreen), getResources().getColor(R.color.lightskyblue)};*/

    private Bitmap bitmap = null;//要显示的头像
    private String text = null;//要显示的文字
    //private int bgColor = Color.GRAY;//背景颜色
    private int textColor = Color.WHITE;//文字颜色
    private int radiusDegree = 10;//圆角度数


    private Paint paintBg = new Paint();
    private Paint pText = new Paint();

    public UserHeadImage(Context context) {
        super(context);
    }

    public UserHeadImage(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public UserHeadImage(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        paintBg.setStyle(Paint.Style.FILL);//充满
        paintBg.setColor(getBgColor());
        paintBg.setAntiAlias(true);// 设置画笔的锯齿效果
        RectF oval3 = new RectF(0, 0, getWidth(), getWidth());// 设置个新的长方形
        canvas.drawRoundRect(oval3, radiusDegree, radiusDegree, paintBg);//第二个参数是x半径，第三个参数是y半径

        if (this.bitmap != null) {
            bitmap = ThumbnailUtils.extractThumbnail(bitmap, getWidth(), getHeight());
            int top = 0;
            int left = 0;
            try {
                top = (getWidth() - bitmap.getWidth()) / 2;
                left = (getHeight() - bitmap.getHeight()) / 2;
            } catch (Exception e) {
            }
            canvas.drawBitmap(ImageUtil.getRoundedCornerBitmap(bitmap, this.radiusDegree), top, left, null);
        } else if (!Util.isEmpty(text)) {
			/*paintBg.setStyle(Paint.Style.FILL);//充满
			paintBg.setColor(getBgColor());
			paintBg.setAntiAlias(true);// 设置画笔的锯齿效果
			RectF oval3 = new RectF(0, 0, getWidth(), getWidth());// 设置个新的长方形
			canvas.drawRoundRect(oval3, radiusDegree, radiusDegree, paintBg);//第二个参数是x半径，第三个参数是y半径*/
            this.drawText(canvas);
        }
        canvas.restore();
    }

    public void drawText(Canvas canvas) {
        int x = getWidth();
        int y = getHeight();

        pText.setColor(getResources().getColor(android.R.color.white));
        //p.setTypeface(font);
        pText.setAntiAlias(true);//去除锯齿
        pText.setFilterBitmap(true);//对位图进行滤波处理
        pText.setTextSize(scalaFonts((int) (getWidth() * 0.7f)));
        float tX = (x - getFontlength(pText, this.text)) / 2;
        float tY = (y - getFontHeight(pText)) / 2 + getFontLeading(pText);
        canvas.drawText(this.text, tX, tY, pText);
    }

    /**
     * 根据屏幕系数比例获取文字大小
     *
     * @return
     */
    private static float scalaFonts(int size) {
        //暂未实现
        return size;
    }

    /**
     * @return 返回指定笔和指定字符串的长度
     */
    public static float getFontlength(Paint paint, String str) {
        return paint.measureText(str);
    }

    /**
     * @return 返回指定笔的文字高度
     */
    public static float getFontHeight(Paint paint) {
        Paint.FontMetrics fm = paint.getFontMetrics();
        return fm.descent - fm.ascent;
    }

    /**
     * @return 返回指定笔离文字顶部的基准距离
     */
    public static float getFontLeading(Paint paint) {
        Paint.FontMetrics fm = paint.getFontMetrics();
        return fm.leading - fm.ascent;
    }


    @Override
    public void setImageBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
        postInvalidate();
    }

    /*@Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
        this.setImageBitmap(bm);
    }*/
    public void setText(String text) {
        if (Util.isEmpty(text)) {
            return;
        }
        if (text.length() > 1) {
            text = text.substring(0, 1);
        }
        this.text = text;
        postInvalidate();
    }

    public void setImageResource(int res) {
        Drawable drawable = getResources().getDrawable(res);
        this.bitmap = ImageUtil.drawableToBitmap(drawable);
        postInvalidate();
    }

    private int getBgColor() {
        //int index = 0;//new Random().nextInt(5);
        //return this.bgColos[index];
        return getResources().getColor(android.R.color.darker_gray);
    }
}
